package com.fenger.bus.service.impl;

import com.fenger.bus.domain.Goods;
import com.fenger.bus.domain.Provider;
import com.fenger.bus.mapper.GoodsMapper;
import com.fenger.bus.mapper.ProviderMapper;
import com.fenger.bus.service.GoodsService;
import com.fenger.bus.vo.GoodsVo;
import com.fenger.bus.vo.ProviderVo;
import com.fenger.sys.contast.SysConstast;
import com.fenger.sys.utils.DataGirdView;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

   @Autowired
   ProviderMapper providerMapper;

    @Override
    @Transactional(readOnly = true)
    public DataGirdView queryAllGoods(GoodsVo goodsVo) {
        Page<Object> page = PageHelper.startPage(goodsVo.getPage(), goodsVo.getLimit());
        List<Goods> goodss = this.goodsMapper.queryAllGoods(goodsVo);

        return new DataGirdView(SysConstast.SUCCESS_CODE,SysConstast.SUCCESS_MSG,page.getTotal(),goodss);
    }

    @Override
    public void addGoods(GoodsVo goodsVo) {
        this.goodsMapper.insertSelective(goodsVo);
    }

    @Override
    public void updateGoods(GoodsVo goodsVo) {
        this.goodsMapper.updateByPrimaryKeySelective(goodsVo);
    }

    @Override
    public void deleteGoods(Integer id) {
        this.goodsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public DataGirdView querySelectedProviderList() {
        try {
            List<Provider> list = this.providerMapper.queryAllProvider(new ProviderVo());
            return new DataGirdView(SysConstast.OPERATOR_SUCCESS_CODE,SysConstast.SUCCESS_MSG,0L,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.OPERATOR_ERROR_CODE,SysConstast.ERROR_MSG,0L,null);

        }
    }
}
